<?php


namespace app\backend\controller\v1;

use app\common\model\Cooperate as CooperateModel;
use think\Request;
use app\backend\validate\CooperateValidate;
use think\exception\ValidateException;

class Cooperate extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $page = input('page/d', 1);
        $size = 10;
        $model = new CooperateModel();
        $datas = $model->where('status', 1)->page($page, $size)->select();
        foreach ($datas as &$v) {
            if (!empty($v['image']))
                $v['image'] = env('APP.YUMING') . $v['image'];
        }
        $rows = count($model->where('status', 1)->select()->toArray());
        $maxPage = ceil($rows / $size);
        return sucessMsg(200, '获取成功！', ['data' => $datas, 'page' => $page, 'maxPage' => $maxPage, 'rows' => $rows]);
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(CooperateValidate::class)->scene('save')->check($postData);
            //dd($postData);
            $model = new CooperateModel();
            if ($model->save($postData))
                return sucessMsg(200, '添加成功');
            return sucessMsg(422, '添加失败');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @return \think\Response
     */
    public function edit()
    {
        //
        $model = new CooperateModel();
        $id = input('get.id/d');
        if (empty($id)) {
            return errMsg(422, 'id不能为空');
        }
        $data = $model->where('id', $id)->find();
        $data['image'] = env('APP.YUMING') . $data['image'];
        if (empty($data))
            return errMsg(422, '无效id');
        return sucessMsg(200, '获取成功！', $data);
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function update()
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(CooperateValidate::class)->scene('update')->check($postData);
            if(strpos($postData['image'],'.com') !== false){
                $postData['image'] = substr($postData['image'],strpos( $postData['image'],"m")+2);
            }            $model = new CooperateModel();
            $cate = $model->where('id', $postData['id'])->find();
            if ($cate->save($postData))
                return sucessMsg(200, '修改成功');
            return sucessMsg(422, '修改失败');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(422, 'id不能为空');
        }
        $data = CooperateModel::where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        if (CooperateModel::destroy($id))
            return sucessMsg(200, '删除成功');
        return errMsg(422, '删除失败');
    }
}